package com.xnfh.home.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.interfaces.SimpleCallback
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.framework.base.BaseFragment
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.framework.bean.FieldDynamicsBean
import com.xnfh.framework.utils.getColorFromResource
import com.xnfh.framework.utils.getStringFromResource
import com.xnfh.home.R
import com.xnfh.home.adapter.FieldDynameicsAdapter
import com.xnfh.home.databinding.FragmentFieldDynamicsBinding
import com.xnfh.home.pop.DiseaseTypePop
import com.xnfh.home.viewmodel.ClubDetailViewModel

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2024/1/5 14:29
 * @UpdateUser: ziheng
 * @UpdateDate: 2024/1/5 14:29
 * @UpdateRemark: 更新说明
 */
class FieldDynamicsFragment : BaseFragment<FragmentFieldDynamicsBinding>(), View.OnClickListener {
    override fun getLayoutID(): Int = R.layout.fragment_field_dynamics

    private lateinit var viewModel: ClubDetailViewModel
    private lateinit var adapter: FieldDynameicsAdapter

    //作物id
    private var cropId: Int? = null
    private var page = 1

    //筛选的动态类型
    private var feedbackType: String? = null

    //筛选的地区id 省、市
    private var provinceId: String? = null
    private var cityId: String? = null

    //筛选框
    private var cropSelectPop: DiseaseTypePop? = null
    override fun initView(view: View, savedInstanceState: Bundle?) {
        val bundleExtra = activity?.intent?.getBundleExtra("bundle")
        cropId = bundleExtra?.getInt("cropId")
        mBind.dynamicsTypeButton.setOnClickListener(this)
        mBind.fieldDynameicsRecycle.layoutManager = LinearLayoutManager(requireContext())
        adapter = FieldDynameicsAdapter()
        mBind.fieldDynameicsRecycle.adapter = adapter
        viewModel.requestCropType()
        mBind.fieldDynameicsSmartRefresh.autoRefresh()
        mBind.fieldDynameicsSmartRefresh.setOnRefreshLoadMoreListener(object :
            OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.requestFieldMessage(cropId, provinceId, cityId, feedbackType, page)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.requestFieldMessage(cropId, provinceId, cityId, feedbackType, page)
            }
        })
    }

    override fun onClick(v: View?) {
        when (v) {
            mBind.dynamicsTypeButton -> {
                if(cropSelectPop == null){
                    cropSelectPop = XPopup.Builder(requireContext()).atView(mBind.dynamicsType).dismissOnTouchOutside(false)
                        .setPopupCallback(object :SimpleCallback(){
                            override fun onShow(popupView: BasePopupView?) {
                                super.onShow(popupView)
                                mBind.icCropUp.setImageResource(R.mipmap.ic_arrow_up)
                            }

                            override fun onDismiss(popupView: BasePopupView?) {
                                super.onDismiss(popupView)
                                mBind.icCropUp.setImageResource(R.mipmap.ic_arrow_down)
                            }
                        })
                        .asCustom(
                            DiseaseTypePop(
                                requireContext(),
                                viewModel.cropTypeList.value?.data
                            )
                        ) as DiseaseTypePop
                }
                cropSelectPop?.setOnListener(object :DiseaseTypePop.onListener{
                    override fun onSure(cropName: String?, cropId: Int?) {
                        cropSelectPop?.dismiss()
                        if(cropName != null){
                            mBind.dynamicsType.setTextColor(getColorFromResource(R.color.color_00C358))
                            mBind.dynamicsType.text = cropName
                            this@FieldDynamicsFragment.cropId = cropId
                            mBind.fieldDynameicsSmartRefresh.autoRefresh()
                        }else{
                            mBind.dynamicsType.setTextColor(getColorFromResource(R.color.black))
                            mBind.dynamicsType.text = getStringFromResource(R.string.str_type_tip)
                            this@FieldDynamicsFragment.cropId = null
                            mBind.fieldDynameicsSmartRefresh.autoRefresh()
                        }
                    }
                })
                cropSelectPop!!.show()
            }
        }
    }

    override fun observe() {
        viewModel = ClubDetailViewModel()
        viewModel.fieldDynamicsBean.observe(this, object : BaseStateObserver<FieldDynamicsBean>() {
            override fun getRespDataSuccess(it: FieldDynamicsBean) {
                super.getRespDataSuccess(it)
                mBind.fieldDynameicsSmartRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
                if (it.total == 0) {
                    adapter.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                    return
                }
                if (page == 1) {
                    adapter.setNewInstance(it.records.toMutableList())
                } else {
                    adapter.addData(it.records)
                }
            }
        })
    }

}